package com.xkstream.client.ui;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;

import com.tencent.bugly.crashreport.CrashReport;
import com.wangjing.permission.annotations.PermissionsDenied;
import com.wangjing.permission.annotations.PermissionsGranted;
import com.wangjing.permission.annotations.PermissionsRationale;
import com.wangjing.permission.annotations.PermissionsRequestSync;
import com.wangjing.permission.api.WJPermission;
import com.xkstream.client.R;
import com.xkstream.client.appmanager.AppManager;
import com.xkstream.client.base.BaseActivity;
import com.xkstream.client.utils.LeakUtil;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

import static com.xkstream.client.ui.StartActivity.PERMISSION_CAMERA;
import static com.xkstream.client.ui.StartActivity.PERMISSION_READ_EXTERNAL_STORAGE;
import static com.xkstream.client.ui.StartActivity.PERMISSION_READ_PHONE_STATE;
import static com.xkstream.client.ui.StartActivity.PERMISSION_RECORD_AUDIO;


/**
 * Created by admin on 2017/5/2.
 */
@PermissionsRequestSync(value = {PERMISSION_CAMERA,
        PERMISSION_RECORD_AUDIO,
        PERMISSION_READ_PHONE_STATE,
        PERMISSION_READ_EXTERNAL_STORAGE
}
        , permissions = {Manifest.permission.CAMERA,
        Manifest.permission.RECORD_AUDIO,
        Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.READ_EXTERNAL_STORAGE})
public class StartActivity extends BaseActivity {
    static final int PERMISSION_CAMERA = 0;
    static final int PERMISSION_RECORD_AUDIO = 1;
    static final int PERMISSION_READ_PHONE_STATE = 2;
    static final int PERMISSION_READ_EXTERNAL_STORAGE = 3;
    int mDenied = 0;

    private CompositeDisposable disposable;

    @Override
    public void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_welcome);
        //ButterKnife.bind(this);
        disposable = new CompositeDisposable();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            WJPermission.syncRequestPermissions(this);
        } else {
            enterMain();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
            grantResults) {
        WJPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    @PermissionsDenied({PERMISSION_CAMERA, PERMISSION_RECORD_AUDIO, PERMISSION_READ_PHONE_STATE,
            PERMISSION_READ_EXTERNAL_STORAGE})
    public void syncDenied(int code) {
        switch (code) {
            case PERMISSION_CAMERA:
                mDenied++;
                break;
            case PERMISSION_RECORD_AUDIO:
                mDenied++;
                break;
            case PERMISSION_READ_PHONE_STATE:
                mDenied++;
                break;
            case PERMISSION_READ_EXTERNAL_STORAGE:
                showToast(R.string.need_permission);
                disposable.add(Observable.timer(1, TimeUnit.SECONDS)
                        .subscribe((l) -> {
                            finish();
                        }));
                break;
            default:
                break;
        }
    }

    @PermissionsGranted({PERMISSION_CAMERA, PERMISSION_RECORD_AUDIO, PERMISSION_READ_PHONE_STATE,
            PERMISSION_READ_EXTERNAL_STORAGE})
    public void syncGranted(int code) {
        switch (code) {
            case PERMISSION_CAMERA:
                break;
            case PERMISSION_RECORD_AUDIO:
                break;
            case PERMISSION_READ_PHONE_STATE:
                break;
            case PERMISSION_READ_EXTERNAL_STORAGE:
                if (mDenied > 0) {
                    showToast(R.string.need_permission);
                    disposable.add(Observable.timer(2, TimeUnit.SECONDS)
                            .subscribe((l) -> {
                                finish();
                            }));
                } else {
                    enterMain();
                }
                break;
            default:
                break;
        }
    }

    private void enterMain() {
        disposable.add( Observable.timer(2, TimeUnit.SECONDS)
                .subscribe((l) -> {
                    jumpToActivity(LoginActivity_V2.class);
                    finish();
                }));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disposable.clear();
        LeakUtil.fixInputMethodManagerLeak(this);
    }
}
